import {diff_match_patch as DiffMatchPatch} from 'diff-match-patch';

const DIFF_IDENTIFIER = {
  INSERTION: -1, // 新增的
  DELETION: 1, // 删除的
  EQUALITY: 0, // 公共的
}

const createDiffMatchPatch = () => {
  const diffMatchPatch = new DiffMatchPatch();
  return {
    DIFF_IDENTIFIER,
    diffText(prevText, currentText){
      return diffMatchPatch.diff_main(prevText, currentText);
    }
  }
}


export function useHighlightCurrentDetail(diffResults){
  const richTextFragments = [];
  diffResults.forEach(val=>{
    const [identifier, text] = val;
    switch (identifier){
      case DIFF_IDENTIFIER.INSERTION:
        richTextFragments.push(`<span style="color:red">${text}</span>`);
        break;
      case DIFF_IDENTIFIER.DELETION:
        richTextFragments.push(`<del style="color:#B7B7B8">${text}</>`);
        break;
      case DIFF_IDENTIFIER.EQUALITY:
        richTextFragments.push(text);
        break;
      default:
        break;
    }
  })
  return richTextFragments.join("");
}


export default createDiffMatchPatch();
